var prefix = ctx + "md/dataImport"
$(document).ready(function () {
	validateRule();
	loadTable();
});

$.validator.setDefaults({
	submitHandler : function() {
		doImport();
	}
});

function doImport() {
	if ("" == $("#tableId").val()) {
		layer.msg("请选择技术元数据表");
		return;
	}
	var form = document.getElementById("signupForm");
	var formData = new FormData(form);
	var fileType = formData .get("file").type;
//	if (fileType != "image/jpeg" && fileType != "image/png" && fileType !="application/pdf") {
//		layer.msg("承诺书只支持jpg,png,pdf格式文件");
//		return;
//	}
	$.ajax({
		cache : true,
		type : "POST",
		url : prefix + "/doImport",
		data : formData, 
		async : false,
		processData: false,
        contentType: false,
        error : function(request) {
			parent.layer.alert("网络超时");
		},
		success : function(data) {
			if (data.code == 0) {
				layer.msg(data.msg);
				$("#file").val("");
			} else {
				layer.alert(data.msg)
			}
		}
	});

}

function loadTable() {
	var html = '<option value="">--选择技术元数据表--</option>';
	$.ajax({
		url : prefix + '/getTechnicalImportTable',
		type: "POST",
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].tableId + '">' 
				+ data[i].targetTableName +((data[i].targetTableComment == null || data[i].targetTableComment == "")?'' : ' ('+data[i].targetTableComment+') ')
				+ '</option>'
			}
			$(".chosen-select").html(html);
			$(".chosen-select").chosen('destroy').chosen({
				search_contains: true,
				maxHeight : 200
			});
		}
	});
}

function download() {
	var tableId = $("#tableId").val();
	if (null == tableId || tableId.length == 0) {
		layer.msg("请选择技术元数据表");
		return;
	}
	window.location.href=prefix + "/download/"+tableId;
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
	})
}

